package com.safety.web.controller;

import com.safety.core.util.Data;
import com.safety.web.entity.Ghostlamp;
import com.safety.web.entity.Ghostlampsuburb;

import com.safety.web.entity.Recommand;
import com.safety.web.service.GhostlampService;
import com.safety.web.service.GhostlampsuburbService;

import com.safety.web.service.RecommandService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by Administrator on 2017/3/1.
 */
@Controller
@RequestMapping("/ghostlamp")
public class GhostlampController {
    @Resource
    private GhostlampService ghostlampService;
    @Resource
    private GhostlampsuburbService ghostlampsuburbService;
    @Resource
    private RecommandService recommandService;
    @RequestMapping("/ghoslaselect")
    @ResponseBody

    public Map<String, Object> ghoslaselect() throws ParseException {
        Map<String, Object> mapa = new LinkedHashMap<String, Object>();
        List<Ghostlamp> list=ghostlampService.ghoslaselect();
        List<Ghostlampsuburb> list1 = ghostlampsuburbService.ghostlampsuburbseclt();
        List<Recommand> quickme = recommandService.recommandselect();
        List<Map<String, Object>> list2 = new ArrayList<Map<String, Object>>();
        List<Map<String, Object>> list3 = new ArrayList<Map<String, Object>>();

        Date date=new Date();
        DateFormat df=new SimpleDateFormat("yyyy-MM-dd");
        String time=df.format(date);
        java.util.Date begin=df.parse(time);
    for (Ghostlamp ca:list){
        for (Ghostlampsuburb me:list1){
                 mapa.put("id",ca.getId());
                 mapa.put("title",ca.getTitle());
                 mapa.put("titleImgurl",Data.URL.base +ca.getTitleImgurl());
                 mapa.put("author",ca.getAuthor());
                 mapa.put("mark",ca.getMark());
                 java.util.Date end = df.parse(ca.getTime());
                 long between=(begin.getTime()-end.getTime())/1000;//除以1000是为了转换成秒
                 long day1=between/(24*3600);
                 mapa.put("time",day1+"天前");
                       if(ca.getId() == me.getAghid()){
                           Map<String, Object> map= new LinkedHashMap<>();
                           map.put("id",me.getId());
                           map.put("artImgurl",Data.URL.base +me.getArtImgurl());
                           map.put("artContent",me.getArtContent());
                           list2.add(map);
                      }
    }
    for (Recommand qui:quickme){
            if(ca.getId()==qui.getRid()){
                Map<String, Object> map3= new LinkedHashMap<>();
                map3.put("id",qui.getId());
                map3.put("img1url", Data.URL.base +qui.getImgone());
                map3.put("img2url",Data.URL.base +qui.getImgtwo());
                map3.put("img3url",Data.URL.base +qui.getImgthree());
                map3.put("recommandFrom",qui.getRecommandFrom());
                map3.put("recommandTitle",qui.getRecommandTitle());
                java.util.Date end = df.parse(qui.getRecommandTime());
                long between=(begin.getTime()-end.getTime())/1000;//除以1000是为了转换成秒
                long day1=between/(24*3600);
                map3.put("time",day1+"天前");
                list3.add(map3);
            }


    }


    }
    mapa.put("recommandList",list3);
    mapa.put("article",list2);
    mapa.put("code",200);

return mapa;
    }
}
